import {favlist,fav,unfav} from '../../utils/api'
import { Toast } from 'vant'

export default {
    namespaced:true,//开启模块    
        state:{
            favlist:[]
        },
        mutations:{
        //收藏相关
            set_favlist( state,params ){ state.favlist=params},//保存收藏列表
            add_fav(state,params){ state.favlist.push(params) },//收藏 参数是shopinfo
            cancel_fav(state,params){ //取消收藏 参数是shopinfo
           let index = state.favlist.findIndex((item)=>{
                    return item.mtWmPoiId==params.mtWmPoiId                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
               })
               if( index != -1 ){
                    state.favlist.splice(index,1)
               }
         }
        },
        actions:{
            //收藏相关
            get_favlist({commit}){//从数据库请求收藏信息；
            let userinfo = JSON.parse(localStorage.getItem('vuex')).user_info

             favlist({userid:userinfo.id}).then((res)=>{
                  console.log(res);
                  if(res.data.code==200){
                       Toast.success(res.data.msg)
                       commit("set_favlist",res.data.list)
                  }
             })
            },
            quest_add_fav({commit},params){//参数是shopinfo
             //先获取用户信息
             let userinfo =JSON.parse(localStorage.getItem('vuex')).user_info
        
             fav({userid:userinfo.id, shopid:params.mtWmPoiId}).then((res)=>{
                  if(res.data.code==200){
                       Toast.success('已收藏')
                       commit('add_fav',params)
                       
                  }else{
                       Toast.success('收藏失败')
                  }
             })
            },
            quest_cancel_fav({commit},params){
                   //先获取用户信息
                   let userinfo =JSON.parse(localStorage.getItem('vuex')).user_info
                   
                   unfav({userid:userinfo.id, shopid:params.mtWmPoiId}).then((res)=>{
                       if(res.data.code==200){
                            Toast.success('已取消收藏')
                            commit('cancel_fav',params)
                            
                       }else{
                            Toast.fail('请求失败')
                       }
                  })
            }
            }
}